package com.lktx.bbfactory.service.Impl;
import cn.hutool.json.JSONUtil;
import com.lktx.bbfactory.entiy.VO.productListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lktx.bbfactory.common.CommonPage;
import com.lktx.bbfactory.common.exception.BizAsserts;
import com.lktx.bbfactory.entiy.DTO.GanttConfigDto;
import com.lktx.bbfactory.entiy.Product;
import com.lktx.bbfactory.mapper.productMapper;
import com.lktx.bbfactory.service.productService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductServiceImpl extends ServiceImpl<productMapper, Product> implements productService {

    @Resource
    private productMapper productMapper;
    @Override
    public CommonPage<productListVo> listPage(Integer projectId, String name, String type, String process, String clientUsername, Integer pageNum, Integer pageSize) {
        Page<productListVo> pageParam = new Page<>(pageNum, pageSize);
        IPage<productListVo> resultPage = productMapper.ProductWithProjectName(pageParam, projectId, name, type, process,clientUsername);
        return CommonPage.restPage(resultPage);
    }

    @Override
    public void addProduct(Product product) {
        System.out.println(product.getProjectId());
        if (product.getProjectId() == null || product.getProjectId().toString().isEmpty()) {
            BizAsserts.fail("未选择所属项目");
        }
        this.save(product);
    }

    @Override
    public void updateProduct(Product product) {
        BizAsserts.notNull(product.getId(), "产品ID不能为空");
        this.updateById(product);
    }

    @Override
    public boolean saveGanttConfig(String id,List<GanttConfigDto> ganttConfigDtos) {
        return this.lambdaUpdate().eq(Product::getId, id).set(Product::getGanttConfig, JSONUtil.toJsonStr(ganttConfigDtos)).update();
    }

}
